package com.byx.chatserver.mapper;

import com.byx.chatserver.dto.FriendGroupDto;
import com.byx.chatserver.dto.LoginDto;
import com.byx.chatserver.entity.Friend;
import com.byx.chatserver.entity.SimpleFriend;
import com.byx.chatserver.entity.User;
import com.byx.chatserver.vo.LoginVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * IUserMapper
 *
 * @author Superb
 * @date 2021/2/7 21:12
 */
@Mapper
public interface IUserMapper {
//    @Results(id = "userMap",value = {
//            @Result(id = true,column = "u_id",property = "id"),
//            @Result(column = "u_login_id",property = "loginId"),
//            @Result(column = "u_nick_name",property = "nickName"),
//            @Result(column = "u_password",property = "password"),
//            @Result(column = "u_sex",property = "sex"),
//            @Result(column = "u_age",property = "age"),
//            @Result(column = "u_description",property = "description"),
//            @Result(column = "u_birthday",property = "birthday"),
//            @Result(column = "u_telephone",property = "telephone"),
//            @Result(column = "u_real_name",property = "realName"),
//            @Result(column = "u_email",property = "email"),
//            @Result(column = "u_avatar",property = "avatar"),
//            @Result(column = "u_vocation",property = "vocation"),
//    })

    /**
     * 通过视图层对象登录
     * @param loginVo 视图层对象
     * @return 数据传输对象
     */
    @Select("SELECT\n" +
            "\tu_id AS id,\n" +
            "\tu_login_id AS loginId,\n" +
            "\tu_nick_name AS nickName,\n" +
            "\tu_avatar AS avatar, \n" +
            "\tu_last_modify_time AS lastModifyTime \n" +
            "FROM\n" +
            "`user` \n" +
            "WHERE\n" +
            "\tu_login_id = #{loginId} and u_password = #{password}")
    LoginDto login(LoginVo loginVo);

    /**
     * 注册用户
     * @param id
     * @param password
     * @param tel
     */
    @Insert("INSERT INTO `user` ( u_login_id, u_telephone, u_password )\n" +
            "VALUES\n" +
            "\t(#{id},#{tel},#{password})")
    void registerByTel(String id, String tel, String password);

    @Insert("INSERT INTO `user` ( u_login_id, u_nick_name, u_email, u_password,u_last_modify_time )\n" +
            "VALUES\n" +
            "\t(#{id},concat('用户',u_login_id),#{email},#{password},#{timestamp})")
    void registerByEmail(@Param("id") String id, @Param("email") String email, @Param("password") String password, @Param("timestamp") Long timestamp);

    @Select("select u_email from `user` where u_login_id = #{loginId}")
    String queryEmailByLoginId(String loginId);

    @Update("update `user` set u_password = #{password} where u_login_id = #{loginId}")
    void changePasswordByEmail(String loginId,String password);

    /**
     * 获取所有未注册号码
     * @return
     */
    @Select("select number from number_pool where number_status=0")
    List<String> getAllUnregisterId();

    /**
     * 根据id查询数据库是否存在此账号
     * @param id
     * @return
     */
    @Select("select id from number_pool where id=#{id}")
    int getIdById(int id);

    @Select("select u_id from user where u_login_id = #{loginId}")
    Long getUserIdByLoginId(String loginId);

    @Select("select u_nick_name from user where u_id = #{userId}")
    String getUserNickNameById(Long userId);

    @Insert("insert into friend_groups(fg_user_id) value (#{userId})")
    void insertFriendGroupByUserId(Long userId);

    @Insert("insert into friend_groups(fg_name,fg_user_id) values (#{groupName},#{userId})")
    void insertFriendGroupByUserIdAndName(String groupName,Long userId);

//    @Select("select u_id from user where u_email=#{email}")
//    Long queryIdByEmail(String email);
}
